package day21.fefei.homework;




import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

class ServerTCP1{

    private static ServerSocket server;
    private static Socket socket;
    static {
        try {
            // 创建一个服务器端
            System.out.println("服务器端已经创建，等待客户端连接");
            server = new ServerSocket(8088);
            socket = server.accept();
            System.out.println("客户端已经连接上，开始通信。。。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //接收信息
    public static void receiveInfo() throws Exception{
        InputStream is = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String receiveText = null;
        while ((receiveText = reader.readLine()) != null) {
            System.out.println("从客户端接收到的信息为：" + receiveText);
            if (receiveText.contains("over")) {
                break;
            }
        }
    }

    // 发送信息
    public static void sendInfo() throws Exception{
        OutputStream os = socket.getOutputStream();
        Scanner scanner = new Scanner(System.in);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        while (true) {
            System.out.println("准备给客户端发送信息：");
            String sendInfo = scanner.nextLine();
//            os.write(sendInfo.getBytes());
            bw.write(sendInfo);
/**
 * ，客户端和服务器端在发送消息时没有添加换行符，导致BufferedReader的readLine()方法无法正确读取信息。
 * readLine()方法是按行读取的，也就是说它会一直读取到换行符为止。
 * 如果发送的信息中没有换行符，readLine()就会一直等待，导致看起来像是收不到信息。
 */
            bw.newLine();//
            bw.flush();
            if (sendInfo.contains("over")) {
                System.out.println("停止给客户端发送信息。。。");
                break;
            }
        }
        socket.shutdownOutput();
    }

    public static void main(String[] args) {
        // 接收客户端的信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    receiveInfo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        // 给客户端发送信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    sendInfo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

public class ClientTCP1 {

    private static Socket socket;

    static {
        try {
            System.out.println("客户端已经创建，开始连接服务器端。。。。");
            socket = new Socket("127.0.0.1",8088);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //接收信息
    public static void receiveInfo() throws Exception{
        InputStream is = socket.getInputStream();
        System.out.println("客户端开始接收服务器端的信息：");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        // 接收一次就结束
        String content = null;
        while ( (content = reader.readLine()) != null) {
            System.out.println("从服务端接收到的信息为：" + content);
            if (content.contains("over")) {
                System.out.println("停止从服务器端接收信息。。。");
                break;
            }
        }
    }

    // 发送信息
    public static void sendInfo() throws Exception{
        OutputStream os = socket.getOutputStream();
        // 创建Socket对象
        Scanner scanner = new Scanner(System.in);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        int count = 0;
        while (true) {
            System.out.println("准备给服务器端发送信息：");
            String sendText = scanner.nextLine();
//            os.write(sendText.getBytes());
            bw.write(sendText);
            bw.newLine();
            bw.flush();
            if (sendText.contains("over")) {
                System.out.println("客户端给服务器端停止发送信息。。。");
                break;
            }
            System.out.println("第" + ++count +"次发送完毕");
        }
        // 每一次写完信息之后，需要写一个终止符
        //socket.shutdownOutput();
    }

    public static void main(String[] args) {
        // 从服务器端接收信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    receiveInfo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        // 给服务器端发送信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    sendInfo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}